(function($) {
	var make = function(content) {
		var panel = $('.content').html(content), icon, link, prefix, ref;
		var ul = $('ul.list', panel);
		var build = '<div id="fontAwesomeLib" class="as"><div class="headbox"><div class="search"><input type="text" name="icontag" placeholder="搜索图标名称"/></div><span class="tag"></span><a class="cancel" href="javascript:;">取消</a><a class="ok" href="javascript:;">确定</a></div>\n';
		build += '<ul class="justify-start">\n';
		$('>li', ul).each(function() {
			link = $('>div>a', $(this)), icon = $('>i', link);
			if (icon.hasClass('fab'))
				prefix = '[b]';
			else if (icon.hasClass('fal'))
				prefix = '[l]';
			else if (icon.hasClass('fas'))
				prefix = '[s]';
			else if (icon.hasClass('fad'))
				prefix = '[d]';
			else
				prefix = '';
			ref = getRef(icon[0].className)
			build += '<li class="' + ref + '"><a class="fonticon" data-ref="' + prefix + ref + '">';
			build += link.html();
			build += '</a></li>\n';
		})
		build += '</ul>\n';
		build += '<div class="result">搜索到个<span class="count"></span>结果</div></div>\n';
		
		saveAs(new Blob([build], {
			type : "application/xml;charset=UTF-8"
		}), 'font-as.html');
	},
	
	getRef = function(exp) {
		var matches = exp.match(/fa(\w)(\s?)fa-([\w|-]*)/gi);
		if (matches) {
			for (var i = 0; i < matches.length; i++) {
				exp = exp.replace(matches[i], function() {
					var ex = /fa(\w)(\s?)fa-([\w|-]*)/gi.exec(matches[i]);
					return ex[3];
				});
			}
			return exp;
		}
		return 'empty';
	};
	
	$(function() {
		/*
		$('input[name=asfile]').on('change', function(e){
			var file = this.files[0];
			var reader = new FileReader();
			reader.readAsText(file);
			reader.onload = function(e) {
				make(this.result);
			}
		})
		*/
		$('button.start').on('click', function() {
			var content = $('.els').val();
			console.log('Start translation');
			make(content)
		})
	});
})(jQuery)